package com.example.common;


import com.example.dao.HnyySscPickingCartonDetailsDTO;

import java.util.List;
import java.util.Map;

public class MyStringUtil {

    /**
     * 获取特定字符个数
     * @param str
     * @param target
     * @return
     */
    public static int countChar(String str, char target) {
        int count = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == target) {
                count++;
            }
        }
        return count;
    }

    /**
     * 动态计算包装比例
     * @param dto
     * @return
     */
    public static String calculateProp(HnyySscPickingCartonDetailsDTO dto) {
        int bigCount = 1; // 默认1个大包装
        int midCount = 0;
        int smlCount = 0;

        if (dto.getBiginvinfo() != null && dto.getBiginvinfo().getMidinvinfo() != null) {
            List<Map> midList = dto.getBiginvinfo().getMidinvinfo();
            midCount = midList.size();
            for (Map<String, Object> midObj : midList) {
                if (midObj instanceof Map) {
                    Map<?, ?> mid = (Map<?, ?>) midObj;
                    Object smlListObj = mid.get("smlinvinfo");
                    if (smlListObj instanceof List) {
                        smlCount += ((List<?>) smlListObj).size();
                    }
                }
            }
        }

        return String.format("%d:%d:%d", bigCount, Math.max(midCount, 1), Math.max(smlCount, 1));
    }


}
